/*
 * Copyright (c) 2010 Mathias Garbe <code@mathias-garbe.de>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "Packet.hpp"

Packet::Packet()
{
    this->type = UnknownPacketType;
    this->parsedTyped = NotParsed;
    this->gameType = Games::NOGAME;
}


Packet::~Packet()
{

}

void Packet::tryParse()
{
    if(this->getMessageLength() == 0)
    {
        this->parsedTyped = NotParsed;
        return;
    }

    char* msg = this->getMessage();
    char* header = 0;
    char* seq = 0;
    
    int i = 0;
    while(i != this->getMessageLength()-2 && this->gameType == Games::NOGAME)
    {
        if(msg[i] == 0x34 && msg[i+1] == 0x12)
        {
            // CNC95
            this->gameType = Games::TD;
            header = &msg[i];
            seq = &msg[i+2];
        }
        else if(msg[i] == 0x35 && msg[i+1] == 0x12)
        {
            // RA95
            this->gameType = Games::RA;
            header = &msg[i];
            seq = &msg[i+2];
        }
        i++;
    }

    if(this->gameType == Games::TD)
    {
        while(i != this->getMessageLength())
        {
            if(header != 0 && msg[i] == 0x0A)
            {
                this->parsedTyped = ChatMessage;
                // Chatmessage
                char *chatnick = &msg[i+1];
                char *chatmessage = &msg[i+12];
                
                std::stringstream tmp1;
                for(int f=0; f<12; f++)
                {
                    if(chatnick[f] >= 32 && chatnick[f] <= 126)
                    {
                        tmp1 << chatnick[f];
                    }
                    else if(chatnick[f] == 0)
                    {
                        break;
                    }
                }
                this->chatNick = tmp1.str();
               
                std::stringstream tmp2;
                for(int f=0; (f+i+12) <= this->getMessageLength(); f++)
                {
                    if(chatmessage[f] >= 32 && chatmessage[f] <= 126)
                    {
                        tmp2 << chatmessage[f];
                    }
                }
                this->chatMessage = tmp2.str();

                std::cout << this->chatMessage << " " << this->chatNick << std::endl;
            }
            i++;
        }
    }
}

void Packet::setType(PacketType type)
{
    this->type = type;
}

void Packet::setAddr(char* addr)
{
    this->addr.clear();
    for(int i=0; i < 8; i++)
    {
        this->addr.push_back(addr[i]);
    }
}

void Packet::setMessage(char* msg, int length)
{
    this->message.clear();
    for(int i=0; i < length; i++)
    {
        this->message.push_back(msg[i]);
    }
    this->tryParse();
}

PacketType Packet::getType()
{
    return this->type;
}

char* Packet::getAddr()
{
    return &this->addr.front();
}

char* Packet::getMessage()
{
    return &this->message.front();
}

int Packet::getMessageLength()
{
    return this->message.size();
}
